package com.mashibing.tank.pojo;

import com.mashibing.tank.factory.BaseBullet;

import java.awt.*;

/**
 * @ClassName Bullet
 * @Description 子弹
 * @Author zhangjunhao
 * @DATE 2023/8/26 18:51
 */
public class RectBullet extends BaseBullet {

    private static final int SPEED = Integer.parseInt((String)PropertyMgr.get("bulletSpeed"));;
    private static final int WIDTH = ResourceMgr.bulletL.getWidth();
    private static final int HEIGHT = ResourceMgr.bulletL.getHeight();
    private int x, y;
    private Dir dir;
    private boolean live = true;
    private TankFrame tankFrame;
    private Group group = Group.BAD;
    Rectangle rectangle = new Rectangle();

    public RectBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tankFrame = tankFrame;
        rectangle.x=this.x;
        rectangle.y=this.y;
        rectangle.width=WIDTH;
        rectangle.height=HEIGHT;

        tankFrame.getMyBullets().add(this);
    }

    public static int getWIDTH() {
        return WIDTH;
    }

    public static int getHEIGHT() {
        return HEIGHT;
    }


    @Override
    public void paint(Graphics g) {

        if(!live){
            tankFrame.getMyBullets().remove(this);
            return;
        }

        switch (dir) {
            case LEFT:
                g.drawImage(ResourceMgr.bulletL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.bulletR, x, y, null);
                break;
            case UP:
                g.drawImage(ResourceMgr.bulletU, x,y,null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.bulletD, x,y,null);
                break;
        }

        move();
    }


    /**
     * @Author zhangjunhao
     * @Description 子弹飞行的过程
     * @Date  2023/8/28
     * @Param
     * @return
     **/
    public void move() {

        /*
        * 没有停止,只能移动,初始是往下
        * */
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }

        //update rectangle
        rectangle.x=this.x;
        rectangle.y=this.y;


        /*
        * 子弹出界就删除
        * */
        if(x<0 || y<0 || x> TankFrame.getGameWidth() || y>TankFrame.getGameHeight()){
            live = false;
        }
    }


    /**
     * @Author zhangjunhao
     * @Description 子弹与坦克的相撞,这个方法写在坦克中也合适
     * @Date  2023/8/28
     * @Param
     * @return
     **/
    @Override
    public void collideWith(Tank tank) {

        if(this.group == tank.getGroup()) {
            return;
        }

        if(rectangle.intersects(tank.rectangle)){
            tank.die();
            this.die();

            int eX = tank.getX() + tank.getWIDTH()/2 - Explode.getWIDTH() / 2;
            int eY = tank.getY() + tank.getHEIGHT()/2 - Explode.getHEIGHT() / 2;

            tankFrame.getExplodes().add(tankFrame.gameFactory.createExplode(eX, eY,tankFrame));

            /*
            * 加入说现在我想玩一个新的爆炸
            * */


        }
    }

    /**
     * @Author zhangjunhao
     * @Description 子弹与坦克相撞后消失
     * @Date  2023/8/28
     * @Param
     * @return
     **/
    private void die() {
        this.live =false;
    }
}
